import 'package:flutter/material.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/common/qxp_style.dart';
import 'package:full_display/unit/screen/screen_utility.dart';

class SingleSelectWidget extends StatefulWidget {
  final List<String> titleList;
  final Function callBack;
  int selectIndex;

  SingleSelectWidget(
      {Key? key,
      required this.titleList,
      required this.callBack,
      this.selectIndex =0})
      : super(key: key);

  @override
  State<SingleSelectWidget> createState() => _SingleSelectWidgetState();
}

class _SingleSelectWidgetState extends State<SingleSelectWidget> {

  late var localIndex ;

  @override
  void initState() {
    localIndex = widget.selectIndex;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          margin: EdgeInsets.only(left: 23.0.W, right: 23.0.W),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: _getTextWidget(),
          ),
        ),
        Container(
          margin: EdgeInsets.only(left: 23.0.W, right: 23.0.W),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: _getIndicatorWidget(),
          ),
        )
      ],
    );
  }

  List<Widget> _getTextWidget() {
    List<Widget> content = [];
    for (int i = 0; i < widget.titleList.length; i++) {
      content.add(GestureDetector(
        onTap: () {
          setState(() {
            widget.selectIndex = i;
            localIndex =i;
          });
            widget.callBack.call( localIndex);
        },
        child: Text(
          widget.titleList[i],
          style: i ==  localIndex
              ? StandardTextStyle.loginTopStyle
              : StandardTextStyle.loginUnSelectStyle,
        ),
      ));
    }
    return content;
  }

  List<Widget> _getIndicatorWidget() {
    List<Widget> content = [];
    for (int i = 0; i < widget.titleList.length; i++) {
      content.add(Visibility(
          visible: i ==  localIndex,
          child: Container(
            margin: EdgeInsets.only(top: 10.0.H),
            width: 130.0.W,
            alignment: Alignment.center,
            child: Container(
              width: 30.0.W,
              height: 4.0.H,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(2),
                  color: ColorPlate.white),
            ),
          )));
    }
    return content;
  }
}
